package homework.exp06;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class I {
    static Scanner cin = null;

    static {
        try {
            cin = new Scanner(new FileInputStream(new File("src/homework/exp06/I.in")));
        } catch (IOException e) {
            cin = new Scanner(System.in);
        }
    }
    static class Edge implements Comparable<Edge>{
        int from,to,weight;

        public Edge(int from, int to, int weight) {
            super();
            this.from = from;
            this.to = to;
            this.weight = weight;
        }

        @Override
        public int compareTo(Edge that) {
            return this.weight-that.weight;
        }


    }

    public static void main(String[] args) {
        n=cin.nextInt();
        Edge[] edges=new Edge[n*n];
        int k=0;
        for(int x=1;x<=n;x++)
            for(int y=1;y<=n;y++){
                int weight=cin.nextInt();
                if(x!=y&&weight==0)weight=Integer.MAX_VALUE/3;
                edges[k++]=new Edge(x,y,weight);
            }
        Arrays.sort(edges);
        init();
        int sum=0;
        int tot=0;
        for(int i=0;i<k;i++){
            if(tot==n-1)break;
            //edges[i]
            int x=edges[i].from;
            int y=edges[i].to;
            if(find(x)==find(y)){
                continue;
            }
            sum+=edges[i].weight;
            ++tot;
            union(x,y);
        }
        System.out.println(sum);
    }

    static int count() {
        int tot = 0;
        for (int x = 1; x <= n; x++)
            if (x == F[x]) {
                ++tot;
            }
        return tot;
    }

    static void union(int x, int y) {
        int tx = find(x);
        int ty = find(y);
        if (tx == ty)
            return;
        F[ty] = tx;
    }

    static int find(int x) {
        return (x == F[x])?x:(F[x] = find(F[x]));
    }

    static int n = 5;
    static int[] F;

    static void init() {
        F=new int[n+1];
        for (int i = 1; i <= n; i++)
            F[i] = i;
    }

}